<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\ShopService;
use Illuminate\Http\Request;

class ShopController extends BaseController
{
    //门店

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new ShopService())->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //一个
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = (new ShopService())->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('shop_name', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['address'] = $request->post('address', '');
        $pas['map'] = $request->post('map', '');
        if (!$pas['title']) {
            return $this->toJson(-2, '门店名称必须的');
        }
        if (!$pas['address']) {
            return $this->toJson(-2, '门店地址必须的');
        }
        if (!$pas['map']) {
            return $this->toJson(-2, '门店坐标必须的');
        }
        if (!$id) {
            $res = (new ShopService())->addOne($pas);
        } else {
            $res = (new ShopService())->modify($id, $pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '门店ID必须的');
        }
        $res = (new ShopService())->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //地址转坐标
    public function addressToMap(Request $request)
    {
        $address = $request->post('address', '');
        $httpUrl = 'https://apis.map.qq.com/ws/geocoder/v1/';
        $httpUrl .= '?address=' . $address;
        $httpUrl .= '&key=JWYBZ-7LK3K-KRNJA-AGOHX-CIZNK-RCFXV';
        $res = $this->setCurl($httpUrl);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        if ($res['status'] != 0) {
           return $this->toJson(-2, $res['message']);
        }
        return $this->toJson(0, '', $res['result']);
    }
}